#include-once

Local $__gWmEasy_tbFlagInsideCallback = DllStructCreate('BOOL')
Local $__gWmEasy_mFnHandlers[]
Local $__gWmEasy_tpData = DllStructCreate('PTR')
Local $__gWmEasy_tData

Func _WmEasy_Register($hWnd, $uMsg, $fnHandler)
	Local $sFnHandler = IsFunc($fnHandler)? FuncName($fnHandler) : String($fnHandler)
	Local $pFnHandler = 0
	If (Not MapExists($__gWmEasy_mFnHandlers, $sFnHandler)) Then
		; Try directly calling the function
		Local $lTest = Call($sFnHandler, 0, -1, 0, 0)
		If (@error = 0xDEAD And @extended = 0xBEEF) Then Return SetError(1, 1, False)

		Local $idFnHandler = DllCallbackRegister($sFnHandler, 'LRESULT', 'HWND;UINT;WPARAM;LPARAM')
		If (Not $idFnHandler) Then Return Return SetError(1, 1, False)
		$pFnHandler = DllCallbackGetPtr($idFnHandler)
		$__gWmEasy_mFnHandlers[$sFnHandler] = $idFnHandler
	EndIf

	If ($pFnHandler = 0) Then Return SetError(1, 1, False)
	; Add the function pointer entry
	; Check if Dllcallback is currently running and be interupted by this function
	; Don't touch if its in middle of callback or we will have serious error here
	If (DllStructGetData($__gWmEasy_tbFlagInsideCallback, 1)) Then Return SetError(1, 1, False)
	; Make a copy of current data
	Local $tData = $__gWmEasy_tData

	$__gWmEasy_tData = $tData
	DllStructSetData($__gWmEasy_tpData, 1) = DllStructGetPtr($__gWmEasy_tData)
EndFunc

Func _WmEasy_Unregister($hWnd, $uMsg, $fnHandler)
	; Make a copy of current data
	Local $tData = $__gWmEasy_tData

	$__gWmEasy_tData = $tData
EndFunc

Func _WmEasy_UnregisterWindow($hWnd)
	; Make a copy of current data
	Local $tData = $__gWmEasy_tData

	$__gWmEasy_tData = $tData
EndFunc

Func _WmEasy_UnregisterMessage($uMsg)
	; Make a copy of current data
	Local $tData = $__gWmEasy_tData

	$__gWmEasy_tData = $tData
EndFunc

